//获取应用实例  
var app = getApp();
Page({
  data: {
    "types": [
      { val: "补水神器", id: 0 }, { val: "美白雪肌", id: 1 }, { val: "魔变cc", id: 2 }, { val: "眼护专家", id: 3 }, { val: "美白雪肌", id: 4 }, { val: "魔变cc", id: 5 }, { val: "逆龄微雕", id: 6 }, { val: "补水神器", id: 7 }, { val: "逆龄微雕", id: 8 }
    ], 
    goods:[
      { img: "//m.360buyimg.com//mobilecms/s276x276_jfs/t3259/113/6049100689/194277/db341a61/58981b78Nccd3d82b.jpg!q70.jpg", val: "水漾氨基酸盈嫩洁面乳", id:1 }, { img: "//m.360buyimg.com//mobilecms/s276x276_jfs/t3259/113/6049100689/194277/db341a61/58981b78Nccd3d82b.jpg!q70.jpg", val: "水漾氨基酸盈嫩洁面乳" }, { img: "//m.360buyimg.com//mobilecms/s276x276_jfs/t3259/113/6049100689/194277/db341a61/58981b78Nccd3d82b.jpg!q70.jpg", val: "水漾氨基酸盈嫩洁面乳" }, { img: "//m.360buyimg.com//mobilecms/s276x276_jfs/t3259/113/6049100689/194277/db341a61/58981b78Nccd3d82b.jpg!q70.jpg", val: "水漾氨基酸盈嫩洁面乳" }, { img: "//m.360buyimg.com//mobilecms/s276x276_jfs/t3259/113/6049100689/194277/db341a61/58981b78Nccd3d82b.jpg!q70.jpg", val: "水漾氨基酸盈嫩洁面乳" }, { img: "//m.360buyimg.com//mobilecms/s276x276_jfs/t3259/113/6049100689/194277/db341a61/58981b78Nccd3d82b.jpg!q70.jpg", val: "水漾氨基酸盈嫩洁面乳" }
    ],
    activeIndex: 0,
    cid:0,
    page:2,
    ptype:''
  },
  onLoad: function (option) {
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Category/index',
      method: 'post',
      data: {},
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //--init data 
        var status = res.data.status;
        if (status == 1) {
          var list = res.data.list;
          var catList = res.data.catList;
          that.setData({
            types: list,
            // goods: catList,
            activeIndex: list[0].id,
            cid: list[0].id
          });

          wx.request({
            url: app.d.ceshiUrl + '/Api/Product/lists',
            method: 'post',
            data: {
              cat_id: list[0].id
            },
            header: {
              'Content-Type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              var shoplist = res.data.pro;
              that.setData({
                goods: shoplist
              })
              console.log(shoplist)
            },
            error: function (e) {
              wx.showToast({
                title: '网络异常！',
                duration: 2000,
              });
            },

          });
          // console.log(res.data.list)
          // console.log(res.data.catList)
          
        } else {
          wx.showToast({
            title: res.data.err,
            duration: 2000,
          });
        }
      },
      error: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000,
        });
      },

    });
  },
  tapType: function (e) {
    var that = this;
    this.setData({
      activeIndex: e.currentTarget.id,
      page:2
    })
    
    wx.request({
      url: app.d.ceshiUrl + '/Api/Product/lists',
      method: 'post',
      data: {
        cat_id: e.currentTarget.id,
        // ptype: ptype
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        var shoplist = res.data.pro;
        that.setData({
          goods: shoplist,
          cid: e.currentTarget.id
        })
      },
      error: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })

  },
  searchProductData: function () {
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Search/searches',
      method: 'post',
      data: {
        keyword: that.data.searchValue,
        uid: app.d.userId,
        page: that.data.page,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        var data = res.data.pro;
        that.setData({
          goods: res.data.pro,
        });
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    });
  },
  doSearch: function () {
    var searchKey = this.data.searchValue;
    this.searchProductData();
  },
  searchValueInput: function (e) {
    var value = e.detail.value;
    this.setData({
      searchValue: value,
    });
    // if (!value && this.data.productData.length == 0) {
    //   this.setData({
    //     hotKeyShow: true,
    //     historyKeyShow: true,
    //   });
    // }
  },
  onReachBottom: function () {
    console.log('加载数据')
    // this.setData({
    //   page: (this.data.page + 10)
    // })
    // this.searchProductData();
  },
  //点击加载更多
  getMore: function (e) {
    var that = this;
    var page = that.data.page;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Product/get_more',
      method: 'post',
      data: {
        page: page,
        // ptype: that.data.ptype,
        cat_id: that.data.cid
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var prolist = res.data.pro;
        if (prolist == '') {
          wx.showToast({
            title: '没有更多数据！',
            duration: 2000
          });
          return false;
        }
        //that.initProductData(data);
        that.setData({
          page: page + 1,
          goods: that.data.goods.concat(prolist)
        });
        //endInitData
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })
  },
})



